package com.example.administrator.pandanotes;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Environment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
/**
 * Created by 于博 on 2018-07-12.
 * 书籍类修改界面
 * 有修改按钮，点击修改书籍类
 */
public class BookClassUpdateActivity extends AppCompatActivity {
    //变量***************************************************************************
    private Button btnClassUpdate = null;
    private TextView tVClassImg = null;
    private String ClassName = null;
    private EditText eTClassName = null;
    private BookClass bc = null;
    private User theUser = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_class_update);
        //初始化及绑定**************************************************************
        eTClassName= (EditText) findViewById(R.id.eTUpdateClassName);
        tVClassImg = (TextView) findViewById(R.id.tVUpdateClassImg);
        btnClassUpdate = (Button) findViewById(R.id.btnUpdateClass);
        //得到值
        bc = (BookClass) getIntent().getSerializableExtra("BookClass");
        ClassName = bc.getClass_name();
        try {
            FileReader r = new FileReader(Environment.getExternalStorageDirectory() + "/pandanotescookies.txt");
            BufferedReader reader = new BufferedReader(r);
            String id = reader.readLine();
            Log.d("UserName:",id);
            if(id != null && (!id.equals(""))){
                List<User> users = DataSupport.where("user_id = ?",id).find(User.class);
                theUser = users.get(0);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.d("得到书籍类：",ClassName);
        Log.d("当前用户：",theUser.getUser_name());
        //显示值
        eTClassName.setText(bc.getClass_name());
        tVClassImg.setText(bc.getBook_class_imageId());
        //函数或事件***********************************************************
        btnClassUpdate.setOnClickListener(new View.OnClickListener() {//修改按钮点击事件，进行书籍类信息更新
            @Override
            public void onClick(View v) {
                List<BookClass> BookClass = DataSupport.where("class_name=?",eTClassName.getText().toString()).find(BookClass.class);//找到书籍类中所填
                //异常处理
                if(BookClass.size()>0)
                {
                    Toast.makeText(BookClassUpdateActivity.this,"已有此书籍类",Toast.LENGTH_SHORT).show();
                }
                else if(eTClassName.getText().toString().length()<1||eTClassName.getText().toString().length()>16)//书籍类名审核
                {
                    Toast.makeText(BookClassUpdateActivity.this,"书籍类名为1-16位",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    bc.setClass_name(eTClassName.getText().toString());
                    bc.setBook_class_imageId(R.drawable.bookclass);
                    theUser.getBookClasses().add(bc);
                    ContentValues values = new ContentValues();
                    values.put("class_name",eTClassName.getText().toString());
                    DataSupport.updateAll(BookClass.class, values, "class_name = ?", ClassName);
                    Toast.makeText(BookClassUpdateActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(BookClassUpdateActivity.this,BookClassActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
    //返回键调用函数，进行上一页*************************************************************
    @Override
    public void onBackPressed()
    {
        this.finish(); //关闭当前Activity
    }
}
